package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.DbUtils;
import com.qianfeng.lingshijia.R;

import java.util.ArrayList;
import java.util.List;

import custom.ShoppingItem;
import utils.DownloadUtils;
import utils.ShoppingCart;

/**
 * Created by Mr.xingkong on 2016/1/18.
 */
public class ShoppingGoodsLvAdapter extends BaseAdapter
{
    Context context;
    List<ShoppingCart> data;
    public ShoppingGoodsLvAdapter(Context context)
    {
        this.context = context;
        data = new ArrayList<>();
    }

    public void setData(List<ShoppingCart> data)
    {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHodler vh;
        if (convertView == null)
        {
            convertView = LayoutInflater.from(context).inflate(R.layout.shoppingitem_lv_layout,null);
            vh = new ViewHodler();
            vh.goodsPic = (ImageView) convertView.findViewById(R.id.shopping_lv_pic);
            vh.title = (TextView) convertView.findViewById(R.id.shopping_lv_title);
            vh.price = (TextView) convertView.findViewById(R.id.shopping_price);
            convertView.setTag(vh);
        }else{
            vh = (ViewHodler) convertView.getTag();
        }

        DownloadUtils.LoadPicture(data.get(position).getPic_url(),vh.goodsPic);
        vh.title.setText(data.get(position).getProductName());
        vh.price.setText("¥"+data.get(position).getPrice());
        return convertView;
    }

    class  ViewHodler
    {
        ImageView goodsPic;
        TextView title;
        TextView price;
    }
}
